namespace SimpleWebServices.Configuration
{
    using System.Configuration;
    using System.Xml;
    using SimpleWebServices;
    using Xunit;

    public sealed class TestingConfigurationFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<TestingConfiguration>()
                .DerivesFrom<object>()
                .IsConcreteClass()
                .IsSealed()
                .HasDefaultConstructor()
                .Implements<IConfigurationSectionHandler>()
                .Result);
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new TestingConfiguration());
        }

        [Fact]
        public void op_Settings()
        {
            Assert.NotNull(TestingConfiguration.Settings());
        }

        [Fact]
        public void op_Settings_string()
        {
            Assert.NotNull(TestingConfiguration.Settings("simplewebservices/testing"));
        }

        [Fact]
        public void op_Create_object_object_XmlNodeNull()
        {
            TestingSettings actual = new TestingConfiguration().Create(null, null, null as XmlNode) as TestingSettings;

            Assert.Empty(actual.Http.Headers.Absent);
        }

        [Fact]
        public void op_Create_object_object_XmlNodeEmpty()
        {
            XmlDocument xml = new XmlDocument();
            xml.LoadXml("<testing />");

            TestingSettings actual = new TestingConfiguration().Create(null, null, xml.DocumentElement) as TestingSettings;

            Assert.Empty(actual.Http.Headers.Absent);
        }

        [Fact]
        public void op_Create_object_object_XmlNode()
        {
            XmlDocument xml = new XmlDocument();
            xml.LoadXml(
                "<testing>" +
                    "<http>" +
                        "<headers>" +
                            "<absent>Server</absent>" +
                            "<absent>X-Powered-By</absent>" +
                        "</headers>" +
                    "</http>" +
                "</testing>");

            TestingSettings actual = new TestingConfiguration().Create(null, null, xml.DocumentElement) as TestingSettings;

            Assert.NotEmpty(actual.Http.Headers.Absent);
        }

        [Fact]
        public void op_Create_object_object_XmlNodeInvalid()
        {
            XmlDocument xml = new XmlDocument();
            xml.LoadXml("<bad-name/>");

            Assert.Throws<ConfigurationErrorsException>(() => new TestingConfiguration().Create(null, null, xml.DocumentElement));
        }
    }
}